package com.dubious.interview.google;

import java.util.List;
import java.util.ArrayList;

public class AddNumbers {

    public static int[] addNumbers(int[] numberA, int[] numberB)
    {
        List<Integer> result = new ArrayList<Integer>();
        
        int maxLength = (int)Math.max(numberA.length, numberB.length);
        int diffLengthA = maxLength - numberA.length;
        int diffLengthB = maxLength - numberB.length;
        int carryOver = 0;
        for(int i = maxLength - 1; i >= 0; i--)
        {
            int digitA = 0;
            int digitB = 0;
            if(i - diffLengthA >= 0)
            {
                digitA = numberA[i - diffLengthA];
            }
            if(i - diffLengthB >= 0)
            {
                digitB = numberB[i - diffLengthB];
            }
            int digitResult = digitA + digitB + carryOver;
            result.add(digitResult % 10);
            carryOver = digitResult / 10;
        }
        
        int[] resultAsArray = new int[result.size()];
        for(int i = result.size() - 1; i >= 0; i--)
        {
            resultAsArray[result.size() - 1 - i] = result.get(i);
        }
        
        return resultAsArray;
    }
}
